const app = getApp()
var common = require("../../common.js")
Page({
  onShow: function () {},
  data: {
    notUploadImg:true,
    inpuDegree:90,
    containerPx: app.globalData.containerPx,
    showTip: false,
    showInput:false,
    qa: [
        {
          q: '怎么设置?', 
          a: '第一步：点击上传按钮，上传合照。第二步：双指缩放图片，单指拖动图片（也可以单指拖动选取框），使得选取框能够充分框住目标。第三步：点击下一步设置目标提示。' },
        {
          q: '我上传的图片安全吗?',
          a: '您所上传的图片仅在你创建的游戏中出现，我们不会挪作他用。'
        },
    ],
    sel: { x: 0, y: 0 },
    img: { x: 0, y: 0 },
    scale: 1,
    src:''
  },
  //处理图片显示
  showTip: function () {
    this.setData({ showTip: true })
  },
  onFocus:function(){
    this.setData({ inpuDegree: 0 })
  },
  onBlur:function(e){
    this.setData({ inpuDegree: 90 })
  },
  hideTip: function () {
    this.setData({ showTip: false, showInput:false })
  },
  onLoad: function () { 
  },
  selectorMove:function(e){
    //console.log('move:------',e.detail);
    this.getRect('#selector');
  },
  imgMove: function (e) {
    //console.log('move:------',e.detail);
    this.getRect('.img');
  },
  //上传图片
  upload:function(){
    var that=this;    
    var imgCount = 1;
    wx.chooseImage({
      count: imgCount,
      sizeType: ['original','compressed'],
      success: function (res) {
        wx.showLoading({
          title: '正在上传',
		      mask:true,
        })
        var tempFilePaths = res.tempFilePaths;
        that.uploadOne(tempFilePaths[0]);    

      }
    })
  },
  uploadOne:function(onePic){  
    var that=this;
    wx.uploadFile({
      url: app.buildURL('c=Index&a=ajaxUpload&tp=group'), //仅为示例，非真实的接口地址
      filePath: onePic,
      name: 'file',
      success: function (res) {
        if (typeof res == 'object') {
          var ret = JSON.parse(res.data);
        } else {
          var unit8Arr = new Uint8Array(res.data.data)
          var encodedString = String.fromCharCode.apply(null, unit8Arr),
            decodedString = decodeURIComponent(escape((encodedString)));//没有这一步中文会乱码
          var ret = JSON.parse(decodedString);
        }
        wx.showToast({
          title: ret.msg,
          icon: 'none'
        })
        if (ret.status == 1) {
          return false;
        }
        wx.getImageInfo({
          src: ret.pic,
          success: function (res) {
            console.log(res);
            var degree=0;
            if(res.width>res.height){
              degree=90;
            }
            that.setData({
              notUploadImg:false,
              degree: degree,
              src: ret.pic,
              imgWidth: res.width,
              imgHeight: res.height
            })
          }
        })
      },
      complete: function (res) {
        wx.hideLoading();
      }
    });     
  },
  //下一步，跳转充值页面
  nextStep: function (e) {
    this.setData({ showInput:true});
  },
  
  nextStep2: function (e) {
    console.log(e.detail);
    var formId = e.detail.formId;
    let target = e.detail.value.target.trim();
    if (target.length<1){
      wx.showToast({
        title: '请指定查找对象',
        image:'../../image/warn.png'
      });
      return false;
    }
    if (this.data.src.length<1){
      wx.showToast({
        title: '请上传集体照片',
        image: '../../image/warn.png'
      });
      return false;
    }

    wx.showLoading({
          title: '正在保存...',
    })

    var that = this;
    
    var x = that.data.sel.x - that.data.img.x;
    var y = that.data.sel.y - that.data.img.y;
    var extra = x+','+y+','+that.data.scale;
    common.request({
      url: 'c=Game&a=editGame&t=group',
      data: {
        formId: formId,
        target:target,
        album: that.data.src,
        extra: extra
      },
      success: function (e) {
        if(e.status==0){
          wx.navigateTo({
            url: '../../pages/suren/money?gid='+e.gid,
          })
        } else if (e.status == 1){
          wx.showModal({
            title: '提示',
            content: e.msg,
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../../pages/suren/home',
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })

        } else{
          wx.showModal({
            title: '错误',
            content: e.msg,
          })
        }
      },
      fail: function (e) {
        console.log(e)
      },
	  complete:function(){
		  wx.hideLoading();
	  },
      method: "POST"
    });

    console.log(e.detail.value.target);
    return;



  },
  onScale:function(e){
    console.log('scale:-----',e)
    this.data.scale=e.detail.scale;
    this.getRect('.img');
  },

  getRect: function (className) {
    console.log(className,'------------------');
    var that = this;
    wx.createSelectorQuery().select(className).boundingClientRect(function (rect) {
      console.log(rect);
      if (rect.id =='selector'){
        that.data.sel.x = rect.left;
        that.data.sel.y = rect.top;
      }else{
        that.data.img.x = rect.left;
        that.data.img.y = rect.top;
      }
    }).exec()
  }
 
})
